package com.ketai.web;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import com.aliyun.oss.ServiceException;
import com.ketai.common.MessageConstant;
import com.ketai.common.Result;
import com.ketai.common.StatusCode;
import com.ketai.pojo.CActivity;
import com.ketai.query.ActivitesQuery;
import com.ketai.service.ActivityService;
import com.ketai.service.impl.ActivityServiceImpl;
import com.ketai.util.DateTimeUtil;
import com.ketai.util.Pages;
import com.ketai.util.SendUtil;
import com.ketai.vo.ActivityVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Author:长沙科泰梦想学院-蓝色小药丸
 * @Mission:打造演说·创客型跨境电商与互联网人才
 * @Description: 研学活动控制类
 * @Date:Create In 2022/8/5 9:39
 **/

@WebServlet("/admin/activity")
public class ActivityServlet extends HttpServlet {

    ActivityService service = new ActivityServiceImpl();

    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = req.getParameter("opr");
        if ("list".equals(opr)) {
            String curr = req.getParameter("page");
            String limit = req.getParameter("limit");
            String start_time = req.getParameter("startTime");
            String end_time = req.getParameter("endTime");
            String title = req.getParameter("title");
            String type = req.getParameter("type");
            String checked = req.getParameter("checked");
            ActivitesQuery query = new ActivitesQuery(curr, limit, start_time, end_time, title, type, checked);
            Pages<ActivityVo> ActivitiePages = service.getAllActivities(query);
            //调用工具类 返回数据给前端
            SendUtil.SendPageJson(resp, ActivitiePages);

        } else if ("del".equals(opr)) {
            String id = req.getParameter("id");
            int result = service.deleteActivityById(id);
            Result r = Result.message(MessageConstant.ACTITY_DELETE_SUCCESS);
            if (result == 0) {
                r = Result.error(StatusCode.SQLERROR, MessageConstant.ACTITY_DELETE_ERROR);
            }

            SendUtil.Sendjson(resp, r);
        } else if ("save".equals(opr)) {
            String title = req.getParameter("title");
            String actity_category_id = req.getParameter("actity_category_id");
            String position = req.getParameter("address");
            String appllyTime = req.getParameter("appllyTime");
            String content = req.getParameter("content");
            String endTime = req.getParameter("endTime");
            String image = req.getParameter("image");
            String peoplenumber = req.getParameter("peoplenumber");
            String startTime = req.getParameter("startTime");
            // 产生字符ID
            String id = DateTimeUtil.dataConverString(new DateTime(), "yyyyMMddHHmmss");
            // 发布人Id 从session中获取
            String pid = "1";
            CActivity activity = new CActivity(id, pid, title, Convert.toDate(appllyTime), Convert.toInt(actity_category_id), position, image, content, Convert.toDate(startTime), Convert.toDate(endTime), Convert.toInt(peoplenumber));
            service.addActivity(activity);
            SendUtil.Sendjson(resp, Result.ok(MessageConstant.ACTITY_SAVE_SUCCESS, 100));
        } else if ("byid".equals(opr)) {
            String id = req.getParameter("id");
            ActivityVo activityVo = service.getActivityById(id);
            SendUtil.SendjsonFormart(resp, activityVo);
        }else if("check".equals(opr)) {
            String  id= req.getParameter("id");
            String open= req.getParameter("open");
            String  desc = req.getParameter("desc");
            //审核人信息  后续从session中获取
            String pid = "1";
           int result= service.checkedActity(id,open,desc,pid);
           if(result>0){
               SendUtil.Sendjson(resp,MessageConstant.ACTITY_CHECK_SUCCESS);
           }else {
               SendUtil.Sendjson(resp,MessageConstant.ACTITY_CHECK_ERROR);
           }

        }else   if ("endlist".equals(opr)) {
            String curr = req.getParameter("page");
            String limit = req.getParameter("limit");
            String start_time = req.getParameter("startTime");
            String end_time = req.getParameter("endTime");
            String title = req.getParameter("title");
            ActivitesQuery query = new ActivitesQuery(curr, limit, start_time, end_time, title);
            Pages<ActivityVo> ActivitiePages = service.getAllEndActivities(query);
            //调用工具类 返回数据给前端
            SendUtil.SendPageJson(resp, ActivitiePages);

        }


    }
}
